package com.qk.manangement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.entity.pojo.Dept;
import com.qk.entity.request.dept.AddDeptRequest;
import com.qk.entity.request.dept.UpdateDeptRequest;
import com.qk.manangement.service.DeptService;
import com.qk.manangement.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public boolean addDept(AddDeptRequest addDeptRequest) {
        Dept dept = new Dept();
        dept.setName(addDeptRequest.getName());
        dept.setStatus(addDeptRequest.getStatus());
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.insertDept(dept) > 0;

    }

    @Override
    public PageInfo<Dept> getDeptList(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 构造查询条件
        Dept queryDept = new Dept();
        queryDept.setName(name);
        queryDept.setStatus(status);

        // 执行查询
        List<Dept> deptList = deptMapper.selectDeptList(queryDept);

        // 返回分页结果
        return new PageInfo<>(deptList);
    }

    @Override
    public boolean deleteDept(Integer id) {
        return deptMapper.deleteDept(id) > 0;
    }

    @Override
    public Dept getById(Integer id) {
        Dept dept = deptMapper.getById(id);
        return dept;
    }

    @Override
    public boolean updateDept(UpdateDeptRequest updateDeptRequest) {
        Dept dept = getById(updateDeptRequest.getId());
        if (dept == null) {
            return false;
        }
        if (updateDeptRequest.getName() != null)
            dept.setName(updateDeptRequest.getName());
        dept.setUpdateTime(LocalDateTime.now());
        boolean success = deptMapper.updateDept(dept);
        return success;
    }

    @Override
    public List<Dept> list() {

        return deptMapper.selectDeptList(null);
    }
}




